(function() {
	var db = require("../../core/db/index");
	var role = require("./role");
	var ResponObj = require("../_include/responObj");
	module.exports = {
		prefix: "/db",
		get: {
			// "/refreshIndex":function (){
			// 	// if(!role.resCheckRole("admin",this)){
			// 	// 	return
			// 	// }

			// 	db.refreshIndex();
			// 	this.body = ResponObj("json","缓存更新成功");
			// },
			// "/all":function () {
			// 	this.body = db._db;
			// }
			"/db_core_change": function() {
				var type = this.request.query.type;
				if (type === "file") {
					if (db._isConnectAble) { //mongodb的接口，导出数据并切换成文件数据库
						db.bak();
						db.removeLock();
					}
					db.changeCore("file");
					this.body = "切换到文件数据库"
				} else if (type === "mongo") {
					db.changeCore("mongo");
					this.body = "切换到Mongodb";
				} else if (type === "mongo_proxy") {
					db.changeCore("mongo_proxy");
					this.body = "切换到MongodbProxy";
				}
			},
			"/view/:table_name": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;
				var query = this.request.query;
				try {
					var query_string = query.query_string ? JSON.parse(query.query_string) : {};
				} catch (e) {
					this.body = ResponObj("error", "JSON Parse Error:" + String(e));
				}
				var num = parseInt(query.num || 20);
				var page = parseInt(query.page || 0);
				this.body = db.find_list(table_name, query_string, num, page);
			},
			"/view/:table_name/:_id": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;
				var _id = this.params._id;
				this.body = db.find_by_id(table_name, _id);
			},
			"/fix/no_id/:table_name": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;

				db.fix_no_id_data(table_name)
				this.body = "NO_ID_DATA【" + table_name + "】修复完成";
			}
		},
		post: {
			"/new/:table_name": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;
				var query = this.request.query;
				var new_obj;
				if (query.body_key) {
					new_obj = JSON.parse(this.request.body[query.body_key]);
				} else {
					new_obj = this.request.body;
				}

				db.insert(table_name, new_obj);
				this.body = db.find_by_id(table_name, _id);
			}
		},
		put: {
			"/update/:table_name/:_id": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;
				var _id = this.params._id;
				var query = this.request.query;
				var new_obj;
				if (query.body_key) {
					new_obj = JSON.parse(this.request.body[query.body_key]);
				} else {
					new_obj = this.request.body;
				}

				db.update(table_name, _id, new_obj, this.request.query.cover);
				this.body = db.find_by_id(table_name, _id);
			}
		},
		delete: {
			"/remove/:table_name/:_id": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;
				var _id = this.params._id;
				this.body = db.find_by_id(table_name, _id);
				db.remove(table_name, _id);
			},
			"/remove_all/:table_name": function() {
				var admin = role.getAdmin(this);
				var table_name = this.params.table_name;
				db.remove_all(table_name)
				this.body = db.find_all(table_name);
			}
		}
	}
}());